package com.wt.controller;

import com.wt.pojo.Distributor;
import com.wt.service.DistributorService;
import com.wt.util.Pager;
import com.wt.util.Pagination;
import com.wt.util.StringUtil;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/auditing")
public class AuditingController {
	@Resource
	private DistributorService distributorService;
	
	
	@RequestMapping(value = "")
    public String index(Model model) throws Exception{
    	/*
		Distributor d = distributorService.getDistributorById();
    	System.out.println(JSONArray.toJSON(d));*/
        return "auditing/list";
    }
	
	/**
	 * 审核列表数据
	 * @param model
	 * @param page
	 * @param pageSize
	 * @param beginDate
	 * @param endDate
	 * @param status 状态
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public String list(Model model, Integer page, Integer pageSize, String beginDate, String endDate,
			Integer status, HttpServletRequest request) {
		Pager pager = new Pager();
		pager.setPageth(page == null ? 1 : page);
		pager.setPageSize(pageSize == null ? Pager.DEFAULT_DISPLAY_PAGETHS_COUNT : pageSize);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        Date end = null;
        Long timeStart = null;
        Long timeEnd = null;
        if (!StringUtil.isEmpty(beginDate) && !StringUtil.isEmpty(endDate)) {
            try {
                start = format.parse(beginDate);
                end = format.parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            timeStart = start.getTime();
            timeEnd = end.getTime() + (24*60*60*1000-1);
            Integer time1 = (int) (timeStart/1000);
            Integer time2 = (int) (timeEnd/1000);
    		Pagination<Distributor> distributorPagination = distributorService.findList(time1, time2, status,
    				pager);
    		model.addAttribute("distributorPagination", distributorPagination);
        }else{
        	Pagination<Distributor> distributorPagination = distributorService.findList(null, null, status,
    				pager);
        	model.addAttribute("distributorPagination", distributorPagination);
        }
      
		return "auditing/list_frag";
	}
	
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ResponseBody
	public Distributor edit( Integer id) {
		Distributor distributor = distributorService.getDistributor(id);
		return distributor;
	}
	/**
	 * 认证审核
	 * @param id
	 * @param status
	 * @param proportion
	 * @return
	 */
	@RequestMapping(value = "updateStatus", method = RequestMethod.POST)
	@ResponseBody
	public int updateStatus( Integer id,Integer status,BigDecimal proportion) {
		Distributor distributor = distributorService.getDistributor(id);
		int result = 0;
		//初审通过
		if(status.equals(2)){
			//如果分成比例没有修改
			if(distributor.getProportion().equals(proportion)){
				return result;
			}else{
				//如果更改分成比例,则更改复核状态为1:待复核
				 result = distributorService.updateAuditing(id,2, proportion, 1);
				return result;
			}
		}else{
			 result = distributorService.updateAuditing(id,3, proportion, 1);
			 return result;
		}
		
	}
	
	
	@RequestMapping(value = "review")
    public String review(Model model) throws Exception{
        return "auditing/reviewList";
    }
	
	/**
	 * 复核列表数据
	 * @param model
	 * @param page
	 * @param pageSize
	 * @param beginDate
	 * @param endDate
	 * @param status 状态
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "reviewList", method = RequestMethod.POST)
	public String reviewList(Model model, Integer page, Integer pageSize, String beginDate, String endDate,HttpServletRequest request) {
		Pager pager = new Pager();
		pager.setPageth(page == null ? 1 : page);
		pager.setPageSize(pageSize == null ? Pager.DEFAULT_DISPLAY_PAGETHS_COUNT : pageSize);
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        Date end = null;
        Long timeStart = null;
        Long timeEnd = null;
		if (!StringUtil.isEmpty(beginDate) && !StringUtil.isEmpty(endDate)) {
            try {
                start = format.parse(beginDate);
                end = format.parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            timeStart = start.getTime();
            timeEnd = end.getTime() + (24*60*60*1000-1);
            Integer time1 = (int) (timeStart/1000);
            Integer time2 = (int) (timeEnd/1000);
            Pagination<Distributor> distributorPagination = distributorService.findReviewList(time1, time2, 1,
    				pager);
    		model.addAttribute("distributorPagination", distributorPagination);
        }else{
            Pagination<Distributor> distributorPagination = distributorService.findReviewList(null, null, 1,
    				pager);
        	model.addAttribute("distributorPagination", distributorPagination);
        }
	
		return "auditing/reviewList_frag";
	}
	
	@RequestMapping(value = "updateReviewStatus", method = RequestMethod.POST)
	@ResponseBody
	public int updateReviewStatus( Integer id,Integer reviewStatus) {
		return distributorService.updateReviewAuditing(id, reviewStatus);
	}
}
